package com.example.myfirstapplication

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.Handler
import android.os.IBinder
import android.os.Message
import android.util.Log
import com.example.myfirstapplication.asyncTask.CommonTask
import com.example.myfirstapplication.asyncTask.WeatherTask
import com.example.myfirstapplication.data.IP_Data
import com.example.myfirstapplication.data.Weather_Data
import com.example.myfirstapplication.service.MyIntentService
import com.example.myfirstapplication.service.MyService
import com.example.myfirstapplication.utils.BaseActivity
import com.example.myfirstapplication.utils.LOG
import com.example.myfirstapplication.utils.Tag
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_thread.*
import kotlin.concurrent.thread

class ThreadActivity : BaseActivity() {

    val Flag_ok = 1
    var callbackData: String = " "
    var wData: Weather_Data? = null

    val handler = object : Handler() {
        override fun handleMessage(msg: Message) {
            // 在这里可以进行UI操作
            Log.i(Tag, "handler Thread:${Thread.currentThread().name}")
            Log.i(Tag, msg.toString())
            when (msg.what) {
                Flag_ok -> {
                    showText_threadLayout.text = callbackData
                }
            }
        }
    }

    lateinit var downloadBinder: MyService.DownloadBinder

    private val connection = object : ServiceConnection {
        override fun onServiceDisconnected(name: ComponentName?) {
            downloadBinder.stop()
        }

        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            downloadBinder = service as MyService.DownloadBinder
            downloadBinder.start()
            downloadBinder.getProgress()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_thread)
        Log.i(Tag, "main Thread:${Thread.currentThread().name}")
        btnActionThread.setOnClickListener {
/*            thread {
                val msg = Message()
                msg.what = Flag_ok
                callbackData = "My Thread is complete,this data is callback"
                handler.sendMessage(msg)
                Log.i(Tag, "sub Thread:${Thread.currentThread().name}")
            }*/
/*            val weatherTask = WeatherTask()
            weatherTask.progressBar = loading_threadLayout
            weatherTask.url = "https://widget.cifuwu.com/weather/"
            weatherTask.setGetDataHandler { innerData ->
                val data = innerData as Weather_Data
                handlerWeatherData(data)
            }
            weatherTask.execute()*/
            val weatherTask = CommonTask()
            weatherTask.progressBar = loading_threadLayout
            weatherTask.url = "https://widget.cifuwu.com/weather"
            weatherTask.setCallbackHandler { innerData ->
                val data = Gson().fromJson(innerData, Weather_Data::class.java)
                handlerWeatherData(data)
            }
            weatherTask.execute()
        }
        val intentService = Intent(this, MyService::class.java)
        btnStartService.setOnClickListener {
            startService(intentService)
        }
        btnStopService.setOnClickListener {
            stopService(intentService)
        }
        btnBundService.setOnClickListener {
            bindService(intentService, connection, Context.BIND_AUTO_CREATE)
        }
        btnUnBundService.setOnClickListener {
            unbindService(connection)
        }
        btnIntentService.setOnClickListener {
            LOG.i("Main Thread is ${Thread.currentThread().name}")
            val intent = Intent(this, MyIntentService::class.java)
            startService(intent)
        }
    }

    private fun handlerWeatherData(data: Weather_Data) {
        Log.i(Tag, "这个时候数据已经回调到外部：\n ${data.toString()}")
        showText_threadLayout.text = data.toString()
    }

    private fun handlerIPData(data: IP_Data) {
        Log.i(Tag, "这个时候数据已经回调到外部：\n ${data.toString()}")
        //showText_threadLayout.text = data.toString()
    }
}